package internet.tcp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务器
public class TcpServer {
    public static void main(String[] args) {
        ServerSocket socket = null;
        Socket accept = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.服务器地址
            socket = new ServerSocket(8999);
            //2.等待客户端连接
            accept = socket.accept();
            //3.读取消息
            is = accept.getInputStream();

            //管道流
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len= is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            try{
                baos.close();
                is.close();
                accept.close();
                socket.close();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}
